package com.billard.controller;


import com.billard.dto.LoginDTO;
import com.billard.Query.UserQuery;
import com.billard.dto.NewPassword;
import com.billard.result.Result;
import com.billard.result.PageResult;
import com.billard.service.UserService;
import com.billard.vo.LoginVO;
import com.billard.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户管理", description = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public Result<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        LoginVO loginVO = userService.login(loginDTO);
        return Result.success(loginVO);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询用户", description = "根据ID查询用户信息")
    @PreAuthorize("@securityPermission.hasPermission('user:view')")
    public Result<Object> getUserById(@PathVariable Long id) {
        return Result.success(userService.getUserById(id));
    }

    /**
     * 创建用户
     */
    @PostMapping("/create")
    @Operation(summary = "创建用户", description = "创建新用户")
    public Result<Void> createUser(@RequestBody User user) {
        userService.createUser(user);
        return Result.success();
    }

    /**
     * 更新用户
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "根据ID更新用户信息")
    @PreAuthorize("@securityPermission.hasPermission('user:update')")
    public Result<Void> updateUser(@PathVariable Long id, @Valid @RequestBody User user) {
        log.info("更新用户ID: {}", id);
        user.setId(id);
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 删除用户（软删除）
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据ID删除用户（软删除）")
    @PreAuthorize("@securityPermission.hasPermission('user:delete')")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success();
    }

    /**
     * 用户分页查询
     */
    @GetMapping("/list")
    @Operation(summary = "用户列表", description = "分页条件查询用户列表")
    @PreAuthorize("@securityPermission.hasPermission('user:list')")
    public Result<PageResult> list(UserQuery query) {
        return Result.success(userService.page(query));
    }

    /**
     * 修改密码
     */
    @PutMapping("/{id}/password")
    @Operation(summary = "修改密码", description = "修改指定用户的登录密码")
    @PreAuthorize("@securityPermission.hasPermission('user:password:update')")
    public Result<Void> changePassword(@PathVariable Long id, @RequestBody NewPassword body) {
        log.info("修改密码用户ID: {}", id);
        userService.updatePassword(id, body.oldPassword, body.newPassword);
        return Result.success();
    }

    /**
     * 为用户分配角色
     */
    @PostMapping("/{id}/role/{roleId}")
    @Operation(summary = "分配用户角色", description = "为指定用户分配一个角色")
    @PreAuthorize("@securityPermission.hasPermission('role:update') or @securityPermission.hasPermission('user:update')")
    public Result<Void> assignRole(@PathVariable Long id, @PathVariable Long roleId) {
        userService.assignRoleToUser(id, roleId);
        return Result.success();
    }

}
